package com.woniu.microcommunityserver.tribal.mapper;

import com.woniu.microcommunityserver.tribal.entity.TopicPosterInfoEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

@Mapper
public interface TopicPosterInfoMapper {
    /**
     * 查询所有贴子
     * @return
     */
    @Select("select id,topic_title,topic_content,fk_interest_tribal_id,reading_quantity,release_time,poster_state," +
            "fk_user_id from topic_poster_info order by release_time desc")
    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column="topic_title",property="topicTitle"),
            @Result(column="topic_content",property="topicContent"),
            @Result(column="fk_interest_tribal_id",property="fkInterestTribalId"),
            @Result(column="reading_quantity",property="readingQuantity"),
            @Result(column="release_time",property="releaseTime"),
            @Result(column="poster_state",property="posterState"),
            @Result(column="fk_user_id",property="fkUserId"),
            @Result(column="fk_interest_tribal_id",property="tribalName",javaType = String.class,
                    one=@One(
                            select="com.woniu.microcommunityserver.tribal.mapper.InterestTribalInfoMapper.tribalName",fetchType = FetchType.EAGER
                    ))
    })
    List<TopicPosterInfoEntity> findAll();

    /**
     * 根据标题查询指定贴子
     * @param map
     * @return
     */
    @Select({"<script> " +
            "select id,topic_title,topic_content,fk_interest_tribal_id,reading_quantity,release_time,poster_state," +
            "fk_user_id from topic_poster_info" +
            "<where>" +
            "<if test='topicTitle!= null'>" +
            "and topic_title like concat('%', #{topicTitle}, '%')"+
            "</if>"+
            "<if test='fkInterestTribalId!= null'>" +
            "and fk_interest_tribal_id = #{fkInterestTribalId}"+
            "</if>"+
            "</where>"+
            "order by release_time desc"+
            "</script>"})
    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column="topic_title",property="topicTitle"),
            @Result(column="topic_content",property="topicContent"),
            @Result(column="fk_interest_tribal_id",property="fkInterestTribalId"),
            @Result(column="reading_quantity",property="readingQuantity"),
            @Result(column="release_time",property="releaseTime"),
            @Result(column="poster_state",property="posterState"),
            @Result(column="fk_user_id",property="fkUserId"),
            @Result(column="fk_interest_tribal_id",property="tribalName",javaType = String.class,
                    one=@One(
                            select="com.woniu.microcommunityserver.tribal.mapper.InterestTribalInfoMapper.tribalName",fetchType = FetchType.EAGER
                    ))
    })
    List<TopicPosterInfoEntity> findAny(Map<String,Object> map);

    /**
     * 删除帖子
     * @param id
     * @return
     */
    @Delete("delete from topic_poster_info where id=#{id}")
    boolean deleteTopic(Integer id);

    /**
     * 根据部落查询帖子
     * @param fkInterestTribalId
     * @return
     */
    @Select("select id,topic_title,topic_content,fk_interest_tribal_id,reading_quantity,release_time,poster_state," +
            "fk_user_id from topic_poster_info where poster_state=0 and fk_interest_tribal_id=#{fkInterestTribalId} order by release_time desc")
    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column="topic_title",property="topicTitle"),
            @Result(column="topic_content",property="topicContent"),
            @Result(column="fk_interest_tribal_id",property="fkInterestTribalId"),
            @Result(column="reading_quantity",property="readingQuantity"),
            @Result(column="release_time",property="releaseTime"),
            @Result(column="poster_state",property="posterState"),
            @Result(column="fk_user_id",property="fkUserId"),
            @Result(column="fk_interest_tribal_id",property="tribalName",javaType = String.class,
                    one=@One(
                            select="com.woniu.microcommunityserver.tribal.mapper.InterestTribalInfoMapper.tribalName",fetchType = FetchType.EAGER
                    ))
    })
    List<TopicPosterInfoEntity> findByInterest(Integer fkInterestTribalId);

    /**
     * 根据id查询贴子
     * @param id
     * @return
     */
    @Select("select id,topic_title,topic_content,fk_interest_tribal_id,reading_quantity,release_time,poster_state," +
            "fk_user_id from topic_poster_info where id=#{id}")
    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column="topic_title",property="topicTitle"),
            @Result(column="topic_content",property="topicContent"),
            @Result(column="fk_interest_tribal_id",property="fkInterestTribalId"),
            @Result(column="reading_quantity",property="readingQuantity"),
            @Result(column="release_time",property="releaseTime"),
            @Result(column="poster_state",property="posterState"),
            @Result(column="fk_user_id",property="fkUserId"),
            @Result(column="fk_interest_tribal_id",property="tribalName",javaType = String.class,
                    one=@One(
                            select="com.woniu.microcommunityserver.tribal.mapper.InterestTribalInfoMapper.tribalName",fetchType = FetchType.EAGER
                    ))
    })
    TopicPosterInfoEntity findByTitle(Integer id);
}
